﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.IO;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;

namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// 外部接口封装：创建CRM发运信息
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CRM_AddLogisticsController : ControllerBase
    {
        private readonly ILogger _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;


        public CRM_AddLogisticsController(ILogger<CRM_AddLogisticsInfoRequestDto> logger, IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration, ILoggerFactory loggerFactory)
        {
            _WMSTokenCheckService = wmsTokenCheckService;
            con = _configuration.GetConnectionString("DDM_DbConnection_ZH");
            _logger = loggerFactory.CreateLogger<CRM_AddLogisticsController>();

        }

        /// <summary>
        ///  外部接口封装：创建CRM发运信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostAddLogistics([FromBody] CRM_AddLogisticsInfoRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            CRM_AddLogisticsInfoResponseDto re = new CRM_AddLogisticsInfoResponseDto();

            string currentDateTime = DateTime.Now.ToString("yyyyMMddHHmmss");
            string systemid = "ecology9";
            string password = "A5CB8E3D73834CC598DDC659C6F22523";
            string Md5str = MethodHelp.GetMD5(systemid+ password+ currentDateTime);
            strparm.header.Md5 = Md5str;
            strparm.header.currentDateTime = currentDateTime;
            strparm.header.systemid = systemid;


            try
            {
                Dictionary<string, string> urldic = new Dictionary<string, string>(); //查询公用dic
                urldic.Add("function_description", "新增物流信息");//查看URL配置表地址描述
                DataTable url_dt = MethodHelp.GetTableInfo("TMS0004", urldic, con);//获取URL地址
                if (url_dt.Rows.Count > 0)
                {

                        string jsonText = JsonConvert.SerializeObject(strparm);
                        var reAgv = DB.httpPost2(url_dt.Rows[0]["interface_url"].ToString()+jsonText, "", "否");
                        CRM_AddLogisticsInfoResponseDto psp = JsonConvert.DeserializeObject<CRM_AddLogisticsInfoResponseDto>(reAgv);
                        if (psp.status != "1")
                        {
                            strMsg = "CRM ERR:" + psp.datajson;
                        }
                    if (psp!=null)
                    {
                        re = psp;
                    }
                }


            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }



            _logger.LogInformation(JsonConvert.SerializeObject(re));
            return new JsonResult(re);

        }

      
    }
}
